package gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import client.ClientConsole;
import client.Configuration;
import client.LogIn;
import client.Main;
import client.MainScreen;
import client.User;
import client.setImage;

public class LogIn_GUI extends JFrame implements ActionListener
{
	final public static int DEFAULT_PORT = 5555;
	public static String serverIP;
    public static ClientConsole conn;
    public Configuration conf;
	public JFrame frame;
	public setImage img;
	
	JButton okButton;
	JButton exitButton;
	JButton config;
	JTextField pass;
	JTextField user;
	JTextField ipNum;
	
	public LogIn_GUI() 
	{		
		img = new setImage("login");
		img.setLayout(null);
		this.setTitle("Zer Lah - LogIn"); 
		this.setPreferredSize(new Dimension(515,550));
		this.setBounds((int)Main.dim.getWidth()/2-260, (int)Main.dim.getHeight()/2-275, 515, 550);
		this.setResizable(false); 
		
		exitButton = new JButton("Exit");
		exitButton.setBounds(365,450,130,50);
		exitButton.addActionListener(this);
		
		config = new JButton("jdbc settings");
		config.setBounds(20, 450, 130, 50);
		config.addActionListener(this); 
		
		okButton = new JButton("Login");
		okButton.setBounds(100, 360, 315, 60);
		okButton.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,20));
		okButton.addActionListener(this);

		ipNum=new JTextField();
		ipNum.setBounds(190,115,185,40);
		ipNum.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		
		user=new JTextField();
		user.setBounds(190,200,185,40);
		user.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		
		pass=new JPasswordField();
		pass.setBounds(190,275,185,40);
		pass.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		
		img.add(okButton);
		img.add(exitButton);
		img.add(config);
		img.add(user);
		img.add(pass);
		img.add(ipNum);
		
       	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       	this.add(img);
       	this.setEnabled(true);
       	this.setVisible(true);
       	this.pack();
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == okButton)
		{			
			LogIn login = new LogIn(user.getText(), pass.getText(), ipNum.getText());
			User usr = new User();
			
			if(ipNum.getText().equals(""))
				serverIP = "localhost";
			else 
				serverIP = ipNum.getText();
			
			conn= new ClientConsole(serverIP, DEFAULT_PORT);
			login.confirmUserData(usr, conn);						
		
			if(LogIn.mainFlag == true)
			{	
				this.setVisible(false);
				this.setEnabled(false);
				
				switch (LogIn.usr.getPriv()) 
				{	
					case 0:  Main.main = new MainScreen(400, 330); break; // net manager 
					case 1:  Main.main = new MainScreen(400, 280); break; //store manager 
					case 2:  Main.main = new MainScreen(400, 330); break; // employee  
					case 3:  Main.main = new MainScreen(400, 380); break; //client support employee
					case 4:  Main.main = new MainScreen(400, 280); break; // expert
					case 5:  Main.main = new MainScreen(400, 430); break; // member
		            case 6:  Main.main = new MainScreen(400, 280); break; // non member
				}
				Main.main.setVisible(true);
			}
		}
		
		if(e.getSource()==config)
		{
			conf = new Configuration();
			this.setEnabled(false);
		}
		if(e.getSource()== exitButton)
		{
			System.exit(0);
		}	
	}
}
